import asyncio
import json
import unittest
import uuid
from datetime import datetime
from types import SimpleNamespace

import httpx
import pytest
from apscheduler.triggers.cron import CronTrigger
from loguru import logger
from rapidocr import RapidOCR
from tzlocal import get_localzone

from service import tisp, wx, cloud_studio
from service.tisp import index


@logger.catch
def a():
	1 / 0


class WxPusherTest(unittest.TestCase):

	def test_uuid(self):
		asyncio.run(cloud_studio.activity_task())

	def test_wx(self):
		headers = {
			"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
			"Referer": "https://aistudio.baidu.com/overview",
			"X-Studio-token": "6C5550BDD600C3456FF13CA0EF9E359097DFCFBDCCF3D31FF945FFFFA9C6979CF944A97463487D5E3E72BDC5E2F961E5",
			"Cookie": 'Hm_up_be6b0f3e9ab579df8f47db4641a0a406=%7B%22user_reg_date%22%3A%7B%22value%22%3A%2220201119%22%2C%22scope%22%3A1%7D%2C%22user_course_rt%22%3A%7B%22value%22%3A%22%E9%9D%9E%E8%AF%BE%E7%A8%8B%E7%94%A8%E6%88%B7%22%2C%22scope%22%3A1%7D%2C%22user_center_type%22%3A%7B%22value%22%3A%225%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22563709%22%2C%22scope%22%3A1%7D%7D; Hm_lvt_6b7a9d245c3be48de953790e7b6aea6b=1734066629; BIDUPSID=5551608471FBE2B016B13D4E6F40C5DD; PSTM=1744330539; jsdk-uuid=a9408d0d-311b-41a5-9bfd-b922bc885436; BDUSS=3VkQ083SFNxRmxEMjFtQjFLSDlKMFppa35nOFZMaElVeTMybDRaQjZXVUFCRjVvRVFBQUFBJCQAAAAAAAAAAAEAAAC44~QrwM~A7s23MTk5MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB3NmgAdzZoM; BDUSS_BFESS=3VkQ083SFNxRmxEMjFtQjFLSDlKMFppa35nOFZMaElVeTMybDRaQjZXVUFCRjVvRVFBQUFBJCQAAAAAAAAAAAEAAAC44~QrwM~A7s23MTk5MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB3NmgAdzZoM; H_WISE_SIDS_BFESS=110085_651904_652168_652682_653504_654015_653957_654358_654364_654295_654381_653708_654044_654448_652904_654666_654812_654921_655035_655094_655104_655050; MAWEBCUID=web_vUhpMsrDOdILxQuvqZvUjQrmfyQCZblLMmZfQjDiKxgbFPTktT; ZFY=1aLONheV:BmQUTu8lt3DehaKH9BiL36T2pHvn:BPKh0ec:C; __bid_n=197c91d037a460854adc5b; newlogin=1; MCITY=-332%3A; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; H_WISE_SIDS=60275_62327_63148_63326_63881_63947_64009_64127_64146_64157_64174_64182_64216_64245_64247_64253_64259_64261_64277_64271_64312_64318_64360_64365_64361_64364_64372; PSINO=1; log_first_time=1754532725322; log_last_time=1754533096216; H_PS_PSSID=60275_62327_63148_63326_63947_64009_64127_64146_64157_64174_64216_64245_64247_64253_64259_64261_64277_64271_64312_64318_64360_64365_64361_64364_64372; BA_HECTOR=8g01a58kah0504800l20ak21840h8n1k9b66025; ab_sr=1.0.1_MTIwZTAyZjY3MDY2OTJhMjE4ZWNhYjc4Mjc3YTE0OGQ2ODcxMzQyNzRlY2E2NTdiZjEwOTRmYzlkZTQ4YjczZWRhNmU4YTkzNjIzMWYwMTYzNjY3OTc2MjA2Yzc4NzQ4OGNkYmNiNDlmYWExMjY2ZjI2OGZjNjM5NWE4MjJkMzE2OGI5YzJiNDczZjIxODRiMDVkNmYwZGRjNTNkM2ZmNzM5NzIyYzYwZjU5OGM4OWI5ZDFhNDQzMjNmNDdjNTAy; BAIDUID=21089FECF00BFE0363518251B963AEC6:FG=1; BAIDUID_BFESS=21089FECF00BFE0363518251B963AEC6:FG=1; ai-studio-ticket=BF871F7C48D84E77A2510E9E20FE2056ED90AF23503D417A9A87540778DBBD8F; Hm_lvt_be6b0f3e9ab579df8f47db4641a0a406=1753670827,1754636091; HMACCOUNT=86F1D2936B70E5B3; ai-studio-lc=zh_CN; Hm_lpvt_be6b0f3e9ab579df8f47db4641a0a406=1754636096; lang=zh; RT="z=1&dm=baidu.com&si=aff5402f-16f3-4122-8dd8-5bc31e8b19a5&ss=me0rhe69&sl=5e&tt=1pt9&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=1ppos4"',
		}
		r = httpx.get("https://aistudio.baidu.com/point/user/info", headers=headers)
		print(r.status_code)
		print(r.json())

	def test_wx2(self):
		headers = {
			"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
			"Referer": "https://aistudio.baidu.com/overview",
			"X-Studio-token": "6C5550BDD600C3456FF13CA0EF9E359097DFCFBDCCF3D31FF945FFFFA9C6979CF944A97463487D5E3E72BDC5E2F961E5",
			"Cookie": 'Hm_up_be6b0f3e9ab579df8f47db4641a0a406=%7B%22user_reg_date%22%3A%7B%22value%22%3A%2220201119%22%2C%22scope%22%3A1%7D%2C%22user_course_rt%22%3A%7B%22value%22%3A%22%E9%9D%9E%E8%AF%BE%E7%A8%8B%E7%94%A8%E6%88%B7%22%2C%22scope%22%3A1%7D%2C%22user_center_type%22%3A%7B%22value%22%3A%225%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22563709%22%2C%22scope%22%3A1%7D%7D; Hm_lvt_6b7a9d245c3be48de953790e7b6aea6b=1734066629; BIDUPSID=5551608471FBE2B016B13D4E6F40C5DD; PSTM=1744330539; jsdk-uuid=a9408d0d-311b-41a5-9bfd-b922bc885436; BDUSS=3VkQ083SFNxRmxEMjFtQjFLSDlKMFppa35nOFZMaElVeTMybDRaQjZXVUFCRjVvRVFBQUFBJCQAAAAAAAAAAAEAAAC44~QrwM~A7s23MTk5MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB3NmgAdzZoM; BDUSS_BFESS=3VkQ083SFNxRmxEMjFtQjFLSDlKMFppa35nOFZMaElVeTMybDRaQjZXVUFCRjVvRVFBQUFBJCQAAAAAAAAAAAEAAAC44~QrwM~A7s23MTk5MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB3NmgAdzZoM; H_WISE_SIDS_BFESS=110085_651904_652168_652682_653504_654015_653957_654358_654364_654295_654381_653708_654044_654448_652904_654666_654812_654921_655035_655094_655104_655050; MAWEBCUID=web_vUhpMsrDOdILxQuvqZvUjQrmfyQCZblLMmZfQjDiKxgbFPTktT; ZFY=1aLONheV:BmQUTu8lt3DehaKH9BiL36T2pHvn:BPKh0ec:C; __bid_n=197c91d037a460854adc5b; newlogin=1; MCITY=-332%3A; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; H_WISE_SIDS=60275_62327_63148_63326_63881_63947_64009_64127_64146_64157_64174_64182_64216_64245_64247_64253_64259_64261_64277_64271_64312_64318_64360_64365_64361_64364_64372; PSINO=1; log_first_time=1754532725322; log_last_time=1754533096216; H_PS_PSSID=60275_62327_63148_63326_63947_64009_64127_64146_64157_64174_64216_64245_64247_64253_64259_64261_64277_64271_64312_64318_64360_64365_64361_64364_64372; BA_HECTOR=8g01a58kah0504800l20ak21840h8n1k9b66025; ab_sr=1.0.1_MTIwZTAyZjY3MDY2OTJhMjE4ZWNhYjc4Mjc3YTE0OGQ2ODcxMzQyNzRlY2E2NTdiZjEwOTRmYzlkZTQ4YjczZWRhNmU4YTkzNjIzMWYwMTYzNjY3OTc2MjA2Yzc4NzQ4OGNkYmNiNDlmYWExMjY2ZjI2OGZjNjM5NWE4MjJkMzE2OGI5YzJiNDczZjIxODRiMDVkNmYwZGRjNTNkM2ZmNzM5NzIyYzYwZjU5OGM4OWI5ZDFhNDQzMjNmNDdjNTAy; BAIDUID=21089FECF00BFE0363518251B963AEC6:FG=1; BAIDUID_BFESS=21089FECF00BFE0363518251B963AEC6:FG=1; ai-studio-ticket=BF871F7C48D84E77A2510E9E20FE2056ED90AF23503D417A9A87540778DBBD8F; Hm_lvt_be6b0f3e9ab579df8f47db4641a0a406=1753670827,1754636091; HMACCOUNT=86F1D2936B70E5B3; ai-studio-lc=zh_CN; Hm_lpvt_be6b0f3e9ab579df8f47db4641a0a406=1754636096; lang=zh; RT="z=1&dm=baidu.com&si=aff5402f-16f3-4122-8dd8-5bc31e8b19a5&ss=me0rhe69&sl=5e&tt=1pt9&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=1ppos4"',
		}
		r = httpx.post("https://aistudio.baidu.com/point/sign", headers=headers)
		print(r.status_code)
		print(r.json())

	@pytest.mark.asyncio
	async def test_async(self):
		await index()

	def test_log(self):
		a()

	def test_j2(self):
		# t = [[1, 3, 4, 5, 56, 6]]
		t = []
		for summary, sender, date_time, *_ in t:
			print(summary, sender, date_time)

	def test_j(self):
		t = [1, 3, 4, 5, 56, 6]
		summary, sender, date_time, *_ = t
		print(summary, sender, date_time)

	def test_iter(self):
		pass

	# iter()

	def test_index(self):
		asyncio.run(tisp.index())

	def test_json(self):
		json_str = """{"data": {"user": {"name": "lisi", "age":10}}}"""
		res = json.loads(json_str, object_hook=lambda d: SimpleNamespace(**d))
		print(res.data.user.age)

	def test_qu(self):
		params = {"cat": "签证状态变更", "sta": 0, "pageNo": 1, "pageSize": 10}
		response = httpx.get(
			"https://tisp.utourworld.com/findList",
			params=params,
			headers={
				"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1",
			},
		)
		print(response.status_code)
		print(response.is_redirect)
		# Headers({'server': '******', 'content-length': '0', 'connection': 'keep-alive', 'location': 'https://tisp.utourworld.com/loginTimeOut', 'date': 'Tue, 01 Jul 2025 06:14:42 GMT'})
		print(response.headers)

	def test_v(self):
		tisp.visa_status_change()

	def test_ocr(self):
		engine = RapidOCR(params={"Global.use_det": False, "Global.use_cls": False})
		result = engine("v_code.png")
		print("".join(result.txts))

	def test_search(self):
		print(wx.search_uid())

	def test_search_v2(self):
		url = "https://wxpusher.zjiecode.com/api/fun/wxuser/v2"
		response = httpx.get(url, params={"appToken": "AT_WP0mxEUjJePMffWOz5jEwXt3pdBvAEus", "page ": 1, "pageSize": 100, "type": 0})
		res = json.loads(str(response.content, encoding="utf-8"))
		print([item["uid"] for item in res["data"]["records"]])
		response.close()

	def test_app_token(self):
		token = "AT_WP0mxEUjJePMffWOz5jEwXt3pdBvAEus"
		url = "https://wxpusher.zjiecode.com/api/send/message"
		input_data = {
			"appToken": token,
			"content": "demo_test",
			"summary": "打卡提醒",
			"contentType": 1,
			"uids": [
				"UID_qr4GGRV23CuOnfS4gqih6uglVjFT",
				# "UID_bf7p1ILmj7VaY6U07WCJ34VpyxOZ",
				# "UID_sbMDW4p6VP7JyEpmger7GjtGreCR",
			],
			"verifyPayType": 0,
		}
		response = httpx.post(url, json=input_data)
		print(str(response.content, encoding="utf-8"))
		response.close()

	def test_send_message(self):
		url = "https://wxpusher.zjiecode.com/api/send/message/simple-push"
		input_data = {
			"content": "demo_test",
			"summary": "打卡提醒",
			"contentType": 1,
			"spt": "SPT_RY73qT3690sF0WpbZbdDAe5FCj30",
		}
		response = httpx.post(url, json=input_data)
		print(str(response.content, encoding="utf-8"))
		response.close()

	def test_get_user_statics(self):
		url = "https://www.rcisyn.net/athena-api-prod/api/attendance/mobile/attendance/staff/getUserStatics"
		input_data = {
			"time": "2025-03-01",
		}

		headers = {
			"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 14_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Html5Plus/1.0 (Immersed/44) uni-app",
			"Authorization": "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NDE0MDIzNjIsInVzZXJfbmFtZSI6IjE4OTAyMTU1MTc5IiwianRpIjoiZWJiZTNjMmYtNTdhYy00ZDAwLTgxZGMtMzE3Mjg0YWY3MDA1IiwiY2xpZW50X2lkIjoiYWRtaW4iLCJzY29wZSI6WyJhbGwiXSwidG9rZW4iOiJyb2NrY2hlY2tsb2dpbl90b2tlbl84MjAyYjAwMTEyNGU0MTgxYjZmNDk4YmE1ZGE3ZDAzOSJ9.U0QmyyQc_AeKouyRy183ntZlKzizmaRls6-2bjVWHCo",
		}
		response = httpx.post(url, json=input_data, headers=headers)
		content_str = str(response.content, encoding="utf-8")
		response.close()
		content = json.loads(content_str)
		data = content["data"]
		print(data)

		now = datetime.now()
		date_time = now.strftime("%Y-%m-%d")
		index = int(now.strftime("%d")) - 1
		# cardFlag 0 未打卡   1 已打卡
		print(data[index]["normalList"][0]["cardFlag"])

	def test_crontab(self):
		crontab: CronTrigger = CronTrigger.from_crontab("40 11 * * 1-5")
		print(dir(crontab))
		next_fire_time = crontab.get_next_fire_time(None, datetime.now())
		print(next_fire_time)

	def test_time(self):
		tz = get_localzone()
		print(tz)

	def test_am(self):
		now = datetime.now()
		print(f"{int(now.hour > 12)=}")
